<?php

/**
 * This is the view file.
 * It users the control file to manipulate data
 *
 */
echo "<html><body>";
include '../db-control/inc/functions.php';
include '../model/User.php';
include '../db-control/dbUser.php';

//update a user if the user is trying to do so
if ($_POST['update'] != '') {
    $id = $_POST['update'];
    $u = new User(); //creates a new instance of User()
    $u->setID($id); //sets the user ID
    $db = new dbUser();
    $exists = $db->checkID($u); //checks to see if the ID is a real user
    if (!$exists) {
        echo "<b>User with username '$username' does not exist...</b>";
    } else {
        $u->setUsername($_POST['username']); //sets the user first name
        $u->setFName($_POST['fname']); //sets the user last name
        $result = $db->updateUser($u); //finally, updates the user with the data we've set
        if (!$result) {
            echo "<b> Could not update the user with the id '$id'...</b>";
        }
    }
} else {
    if (isset($_POST['add'])) {
        $u = new User();
        $u->setUsername($_POST['username']);
        $u->setFName($_POST['fname']);
        $db = new dbUser();
        $result = $db->addUser($u);
        if (!$result) {
            echo "<b>Please enter some user information</b>";
        }
    }

    if (isset($_POST['delete_clicked'])) {
        $u = new User(); //creates a new User object (i.e. creates a new user)
        $u->setID($_POST['deleteUser']);
        $db = new dbUser();
        $result = $db->deleteUser($u);
        if (!result) {
            echo "<b> Could not delete user with username '$_POST[deleteUsername]'...</b>";
        }
    }
}


//displays the web form used for adding a new user
echo <<<_FORM


<form action='viewUser.php' method='post'>
<table>
    <tr><td colspan='2'>Specify a username to change a user's data</td><td><input type='text' size='4' name='update' /></td></tr>
    <tr><td>Username:</td><td><input type='text' value='' name='username' /></td></tr>
    <tr><td>First Name:</td><td><input type='text' value='' name='fname' /></td></tr>
    <tr><td></td><td align='right'><input type='submit' value='Add User' name='add'/></td></tr>
</table>
</form>


_FORM;

displayUsers(); //displays the user list


echo "</body></html>";

function displayUsers() {
    $db = new dbUser(); //gets all the users from the database
    $result = $db->getAllUsers();
    echo "<table width='200px'>";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "<tr><td>$row[id]</td><td></td></tr>";
        echo "<tr><td align='right'>Username:</td><td align='left'>$row[username]</td></tr>";
        echo "<tr><td align='right'>First Name:</td><td align='left'>$row[fname]</td></tr>";
        echo "<tr><td colspan='2' align='right'><form action='viewUser.php' method='post'><input type='hidden' value='$row[id]' name='deleteUser' /><input type='submit' value='Delete' name='delete_clicked' /></form></td><td></td></tr>";
        echo "<tr><td colspan='2'><hr /></td><td></td></tr>";
    }
    echo "</table>";
}

?>
